package com.neusoft.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.util.StringUtils;
import com.neusoft.domain.Employee;
import com.neusoft.domain.PageResult;
import com.neusoft.exception.BusinessException;
import com.neusoft.mapper.EmployeeMapper;
import com.neusoft.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neusoft.domain.ResultCode;
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public PageResult<Employee> getEmployeeByPage(Integer pageNum, Integer pageSize, String account, String nickname, String telephone) {
        if(pageNum==null||pageSize==null){
            throw new BusinessException("页码和每页显示条数不能为空", ResultCode.PAGE_ERROR);
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<Employee>page = (Page<Employee>)employeeMapper.getEmployeeByPage(account,nickname,telephone);
        PageResult<Employee> pageResult = new PageResult<>(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void saveEmployee(Employee employee) {
        if(StringUtils.isEmptyOrWhitespaceOnly(employee.getAccount())){
            throw new BusinessException("员工账号不能为空",ResultCode.PARAM_ERROR);
        }
        employee.setPassword("123456");
        employee.setIs_del(0);
        employeeMapper.saveEmployee(employee);
        System.out.println("保存员工成功");

    }

    @Override
    public void updateEmployee(Employee employee) {
        if(StringUtils.isEmptyOrWhitespaceOnly(employee.getAccount())){
            throw new BusinessException("员工账号不能为空",ResultCode.PARAM_ERROR);
        }
        employeeMapper.updateEmployee(employee);
        System.out.println("保存修改成功");
    }

    @Override
    public void updateEmployeeStatus(Employee employee) {

        employeeMapper.updateEmployeeStatus(employee);
        System.out.println("修改员工状态成功");
    }
}
